package com.bsu.ascm.operation;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bsu.ascm.R;
import com.bsu.ascm.controls.ListViewTable;
import com.bsu.ascm.controls.ListViewTable.ListViewColumn;
import com.bsu.ascm.http.AscmHttpClient;
import com.bsu.ascm.tool.AscmEnvironment;

public class ConfirmKanban extends Activity{
	private ListViewTable lvt = null;										//数据表格控件
	private AscmHttpClient http = new AscmHttpClient();						//http请求
	private XmlPullParserFactory xpp_factory = null;						//xmlpull工厂
	private XmlPullParser xpp = null;										//xmlpull解析
	
	private String NO = "";													//物料编号
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);						//设置横屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);											//无title  
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,    					//全屏
                       WindowManager.LayoutParams. FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.confirmkanban);
		
		init(this.getIntent().getExtras());
	}
	/**
	 * 初始化函数
	 */
	private void init(Bundle bundle){
		//初始化xml解析
		try{
			xpp_factory = XmlPullParserFactory.newInstance();
			xpp = xpp_factory.newPullParser();
		}catch(XmlPullParserException e){
			e.printStackTrace();
		}
		
		//初始化头数据
		TextView tv_ck_no = (TextView)findViewById(R.id.tv_ck_no);
		NO = bundle.getString("NO");
		tv_ck_no.setText(NO);
		TextView tv_ck_reqtime = (TextView)findViewById(R.id.tv_ck_reqtime);
		tv_ck_reqtime.setText(bundle.getString("REQTIME"));
		TextView tv_ck_tono = (TextView)findViewById(R.id.tv_ck_tono);
		tv_ck_tono.setText(bundle.getString("TONO"));
		
		//按钮响应事件
		Button bt_ck_confirm = (Button)findViewById(R.id.bt_ck_confirm);
		bt_ck_confirm.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		//准备表格标题数据
		List<ListViewColumn> cols = new ArrayList<ListViewColumn>();
		int avgwidth = AscmEnvironment.getInstance().getDisplayWidth()/3;
		cols.add(new ListViewColumn("ITEMNO","物料",avgwidth));
		cols.add(new ListViewColumn("ITEMDESCR","名称",avgwidth+40));
		cols.add(new ListViewColumn("QTY","数量",avgwidth-40));
		//准备表格数据
		final List<HashMap<String,String>> tabledata = doRequest();
		
		lvt = new ListViewTable(this);
		lvt.makeTable(this, cols, tabledata);
		
		LinearLayout ll_ck_body = (LinearLayout)this.findViewById(R.id.ll_ck_body);
		ll_ck_body.addView(lvt);
	}
	/**
	 * 请求当前看板的物料数据
	 */
	private List<HashMap<String,String>> doRequest(){
		List<HashMap<String,String>> rettable = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> sortmap = null;
		String xml = "";
		try{
			xml = http.query("mobile_KanbanInfo", "NO="+NO);
			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();
			while(eventType!=XmlPullParser.END_DOCUMENT){
				String nodeName = xpp.getName();
				switch(eventType){
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if("Table".equals(nodeName))
						sortmap = new HashMap<String,String>();
					else if("ITEMNO".equals(nodeName))
						sortmap.put("ITEMNO", xpp.nextText());
					else if("ITEMDESCR".equals(nodeName))
						sortmap.put("ITEMDESCR", xpp.nextText());
					else if("QTY".equals(nodeName))
						sortmap.put("QTY", xpp.nextText());
					break;
				case XmlPullParser.END_TAG:
					if("Table".equals(nodeName)){
						rettable.add(sortmap);
					}
					break;
				default:
					break;
				}
				eventType = xpp.next();
			}
		} catch(ClientProtocolException e){
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return rettable;
	}
}
